package com.treasure.web.controller.corp.admin;

import com.treasure.common.model.PageResult;
import com.treasure.common.model.SessionUser;
import com.treasure.common.model.SessionUserHolder;
import com.treasure.common.reponse.Response;
import com.treasure.common.types.Corp;
import com.treasure.web.command.base.PageBaseQuery;
import com.treasure.web.command.request.CorpEmailAccountEditRequest;
import com.treasure.web.command.request.CorpEmailTestRequest;
import com.treasure.web.command.request.CorpInfoEditRequest;
import com.treasure.web.command.request.EditUserPasswordRequest;
import com.treasure.web.command.response.*;
import com.treasure.web.service.*;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * 企业账户信息（企业管理后台）
 *
 * @author sunhongmin
 * @date 2023/11/11 10:56
 * @description 企业账户信息
 */
@RestController
@RequestMapping("/treasure/corp/account")
@RequiredArgsConstructor
public class CorpAccountController {

    private final ICorpAccessTokenService corpAccessTokenService;
    private final IAccountService accountService;
    private final ICorpEmailAccountService corpEmailAccountService;
    private final ICorpInfoService corpInfoService;

    /**
     * 企业基本信息
     *
     * @return
     */
    @PostMapping("/corp/info")
    public Response<CorpInfoResponse> getCorpInfo() {
        return Response.success(corpInfoService.getCorpInfo());
    }

    /**
     * 企业信息编辑
     *
     * @param corpInfoEditRequest
     * @return
     */
    @PostMapping("/corp/edit")
    public Response<String> editCorpInfo(@RequestBody CorpInfoEditRequest corpInfoEditRequest) {
        corpInfoService.corpInfoEdit(corpInfoEditRequest);
        return Response.success();
    }

    /**
     * 获取企业授权信息（默认将为企业自动授权）accountToken
     */
    @PostMapping("/accountToken")
    public Response<CorpAccountTokenResponse> getCorpAccountInfo() {
        SessionUser sessionUser = SessionUserHolder.getAndCheckSessionUser();
        return Response.success(corpAccessTokenService.getCorpAccountTokenInfo(new Corp(sessionUser.getCorpId())));
    }

    /**
     * 企业用户列表
     *
     * @param pageBaseQuery
     * @return
     */
    @PostMapping("/user/list")
    public Response<PageResult<CorpUserInfoResponse>> userList(@RequestBody PageBaseQuery pageBaseQuery) {
        PageResult<CorpUserInfoResponse> result = accountService.userList(pageBaseQuery);
        return Response.success(result);
    }

    /**
     * 编辑用户密码
     *
     * @param editUserPasswordRequest
     * @return
     */
    @PostMapping("/edit/user/password")
    public Response<String> editUserPassword(@RequestBody EditUserPasswordRequest editUserPasswordRequest) {
        accountService.editUserPassword(editUserPasswordRequest);
        return Response.success();
    }

    /**
     * 获取企业邮箱配置
     *
     * @return
     */
    @PostMapping("/email/config")
    public Response<CorpEmailAccountResponse> getCorpEmailAccount() {
        SessionUser sessionUser = SessionUserHolder.getAndCheckSessionUser();
        return Response.success(corpEmailAccountService.getEmailAccount(new Corp(sessionUser.getCorpId())));
    }


    /**
     * 企业邮箱测试
     *
     * @param corpEmailTestRequest
     * @return
     */
    @PostMapping("/email/test")
    public Response<String> testCorpEmail(@RequestBody CorpEmailTestRequest corpEmailTestRequest) {
        return Response.success(corpEmailAccountService.corpEmailTest(corpEmailTestRequest));
    }


    /**
     * 保存企业邮箱配置
     *
     * @param corpEmailAccountEditRequest
     * @return
     */
    @PostMapping("/email/save")
    public Response<String> editEmailAccount(@RequestBody @Validated CorpEmailAccountEditRequest corpEmailAccountEditRequest) {
        SessionUser sessionUser = SessionUserHolder.getAndCheckSessionUser();
        corpEmailAccountService.editEmailAccount(new Corp(sessionUser.getCorpId()), corpEmailAccountEditRequest);
        return Response.success();
    }

}
